package com.inforich.building.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.inforich.building.domain.RubbishType;
import com.inforich.building.mapper.RubbishTypeMapper;
import com.inforich.building.service.IRubbishTypeService;
import com.inforich.common.core.domain.TreeSelect;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;

/**
 * 垃圾类型Service业务层处理
 *
 * @author hjm
 * @date 2022-10-29
 */
@Service
public class RubbishTypeServiceImpl extends ServiceImpl<RubbishTypeMapper, RubbishType> implements
    IRubbishTypeService {

  @Override
  public Page<RubbishType> pageRubbishType(Page<RubbishType> page, Map<String, Object> map) {
    return baseMapper.pageRubbishType(page, map);
  }

  @Override
  public List<RubbishType> listRubbishType(Map<String, Object> map) {
    return baseMapper.listRubbishType(map);
  }

  @Override
  public List<TreeSelect> selectRubbishTypeTreeList(List<RubbishType> list) {
    // 父节点
    List<RubbishType> parentList = list.stream().filter(l -> l.getParentId() == null)
        .collect(Collectors.toList());
    // 父节点树
    List<TreeSelect> tree = convert(parentList);
    // 根据父类型分组
    Map<Long, List<RubbishType>> map = list.stream()
        .filter(b -> b.getParentId() != null)
        .collect(Collectors.groupingBy(RubbishType::getParentId));
    recursionFn(tree, map);
    return tree;
  }

  @Override
  public List<TreeSelect> selectRubbishTypeTreeListByCode(String code) {
    RubbishType rubbishType = getOne(new LambdaQueryWrapper<RubbishType>()
        .eq(RubbishType::getCode, code));
    if (rubbishType == null) {
      return Lists.newArrayList();
    }
    List<RubbishType> parentList = this.list(new LambdaQueryWrapper<RubbishType>()
        .eq(RubbishType::getParentId, rubbishType.getId()));
    List<RubbishType> list = list();
    // 父节点树
    List<TreeSelect> tree = convert(parentList);
    // 根据父类型分组
    Map<Long, List<RubbishType>> map = list.stream()
        .filter(b -> b.getParentId() != null)
        .collect(Collectors.groupingBy(RubbishType::getParentId));
    recursionFn(tree, map);
    return tree;
  }

  /**
   * 对象->树对象
   */
  private List<TreeSelect> convert(List<RubbishType> list) {
    return list.stream().map(l -> {
      TreeSelect t = new TreeSelect();
      t.setId(l.getId());
      t.setLabel(l.getName());
      return t;
    }).collect(Collectors.toList());
  }

  /**
   * 递归列表
   */
  private void recursionFn(List<TreeSelect> tree, Map<Long, List<RubbishType>> map) {
    tree.forEach(t -> {
      List<RubbishType> children = map.get(t.getId());
      if (CollUtil.isNotEmpty(children)) {
        List<TreeSelect> treeChildren = convert(children);
        t.setChildren(treeChildren);
        recursionFn(treeChildren, map);
      }
    });
  }

}
